﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Model;
using System.Data.OleDb;

namespace Thep
{
    public partial class FrmThongSoLienKet : Form
    {
        public FrmThongSoLienKet()
        {
            InitializeComponent();
        }

        private string ThongSoID = "";

        #region Database
        OleDbConnection conn;
        OleDbCommand cmd;

        public void Connect()
        {
            string connectstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Application.StartupPath + "\\Database.a;Persist Security Info=False";
            conn = new OleDbConnection(connectstring);
            cmd = conn.CreateCommand();
        }
        #endregion

        private string thongsoID = "";

        #region Methods

        public void ActiveButton(bool them, bool luu, bool xoa, bool huy)
        {
            btnThem.Enabled = them;
            btnLuu.Enabled = luu;
            btnXoa.Enabled = xoa;
            btnHuy.Enabled = huy;
        }

        public void ActiveText(bool flag)
        {
            txtTenThongSoLienKet.Enabled = flag;
            txtBeRongSuon.Enabled = flag;
            txtChieuCaoDuongHanBung.Enabled = flag;
            txtChieuCaoDuongHanCanh.Enabled = flag;
            txtChieuCaoMuiTonSan.Enabled = flag;
            txtChieuDayBeTong.Enabled = flag;
            txtChieuDayDaiOc.Enabled = flag;
            txtChieuDayLopBaoVe.Enabled = flag;
            txtChieuDayMu.Enabled = flag;
            txtChieuDaySuon.Enabled = flag;
            txtDienTichBuLong.Enabled = flag;
            txtDoCung.Enabled = flag;
            txtDuongKinhTamDem.Enabled = flag;
            txtHamLuongCotThepSan.Enabled = flag;
            txtKhoangCach.Enabled = flag;
            txtKhoangCachBuLongCanhCot.Enabled = flag;
            txtKhoangCachBuLongDung.Enabled = flag;
            txtKhoangCachBulongNgang.Enabled = flag;
            txtKhoangCachBuLongSuonDuoi.Enabled = flag;
            txtKhoangCachBuLongSuonTren.Enabled = flag;
            grcThongSoLienKet.Enabled = !flag;
        }

        public void CleanText()
        {
            txtTenThongSoLienKet.Text = "";
            txtBeRongSuon.Text = "";
            txtChieuCaoDuongHanBung.Text = "";
            txtChieuCaoDuongHanCanh.Text = "";
            txtChieuCaoMuiTonSan.Text = "";
            txtChieuDayBeTong.Text = "";
            txtChieuDayDaiOc.Text = "";
            txtChieuDayLopBaoVe.Text = "";
            txtChieuDayMu.Text = "";
            txtChieuDaySuon.Text = "";
            txtDienTichBuLong.Text = "";
            txtDoCung.Text = "";
            txtDuongKinhTamDem.Text = "";
            txtHamLuongCotThepSan.Text = "";
            txtKhoangCach.Text = "";
            txtKhoangCachBuLongCanhCot.Text = "";
            txtKhoangCachBuLongDung.Text = "";
            txtKhoangCachBulongNgang.Text = "";
            txtKhoangCachBuLongSuonDuoi.Text = "";
            txtKhoangCachBuLongSuonTren.Text = "";
        }

        public DataTable LoadThongSoLienKet()
        {
            Connect();
            DataTable thongso = new DataTable();
            thongso.Columns.Add("ThongSoLienKetID", typeof(int));
            thongso.Columns.Add("TenThongSoLienKet", typeof(string));
            try
            {
                cmd.CommandText = "SELECT * FROM ThongSoLienKet";
                cmd.CommandType = CommandType.Text;
                conn.Open();

                OleDbDataReader reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    DataRow dr = thongso.NewRow();
                    dr["ThongSoLienKetID"] = int.Parse(reader["ThongSoLienKetID"].ToString());
                    dr["TenThongSoLienKet"] = reader["TenThongSo"].ToString();
                    thongso.Rows.Add(dr);
                }
                return thongso;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }
        }

        public ThongSoLienKet FindThongSoLienKet(int id)
        {
            try
            {
                Connect();
                ThongSoLienKet thongso = new ThongSoLienKet();
                cmd.CommandText = "SELECT * FROM ThongSoLienKet WHERE ThongSoLienKetID=" + id;
                cmd.CommandType = CommandType.Text;
                conn.Open();
                OleDbDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    thongso.ThongSoLienKetID = int.Parse(reader["ThongSoLienKetID"].ToString());
                    thongso.TenThongSo = reader["TenThongSo"].ToString();
                    thongso.ChieuCaoDuongHanBung = float.Parse(reader["ChieuCaoDuongHanBung"].ToString());
                    thongso.ChieuCaoDuongHanCanh = float.Parse(reader["ChieuCaoDuongHanCanh"].ToString());
                    thongso.BuLongDenMepTrenSuon = float.Parse(reader["BuLongDenMepTrenSuon"].ToString());
                    thongso.BuLongDenMepDuoiSuon = float.Parse(reader["BuLongDenMepDuoiSuon"].ToString());
                    thongso.BuLongDenMepNgoaiCanhCot = float.Parse(reader["BuLongDenMepNgoaiCanhCot"].ToString());
                    thongso.BuLongPhuongNgang = float.Parse(reader["BuLongPhuongNgang"].ToString());
                    thongso.BuLongPhuongDung = float.Parse(reader["BuLongPhuongDung"].ToString());
                    thongso.BeRongSuon = float.Parse(reader["BeRongSuon"].ToString());
                    thongso.ChieuDaySuon = float.Parse(reader["ChieuDaySuon"].ToString());
                    thongso.DienTichBuLong = float.Parse(reader["DienTichBuLong"].ToString());
                    thongso.ChieuDayMu = float.Parse(reader["ChieuDayMu"].ToString());
                    thongso.ChieuDayDaiOc = float.Parse(reader["ChieuDayDaiOc"].ToString());
                    thongso.DuongKinhTamDem = float.Parse(reader["DuongKinhTamDem"].ToString());
                    thongso.ChieuDayBeTong = float.Parse(reader["ChieuDayBeTong"].ToString());
                    thongso.ChieuCaoMuiTon = float.Parse(reader["ChieuCaoMuiTon"].ToString());
                    thongso.ChieuDayLopBaoVe = float.Parse(reader["ChieuDayLopBaoVe"].ToString());
                    thongso.HamLuongCotThep = float.Parse(reader["HamLuongCotThep"].ToString());
                    thongso.DoCung = float.Parse(reader["DoCung"].ToString());
                    thongso.KhoangCach = float.Parse(reader["KhoangCach"].ToString());

                }


                return thongso;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }
        }

        public void Insert(ThongSoLienKet thongso)
        {
            try
            {
                cmd.CommandText = "INSERT INTO ThongSoLienKet(TenThongSo," +
                                                             "ChieuCaoDuongHanBung," +
                                                             "ChieuCaoDuongHanCanh," +
                                                               "BuLongDenMepTrenSuon," +
                                                               "BuLongDenMepDuoiSuon," +
                                                               "BuLongDenMepNgoaiCanhCot," +
                                                               "BuLongPhuongNgang," +
                                                               "BuLongPhuongDung," +
                                                               "BeRongSuon," +
                                                               "ChieuDaySuon," +
                                                               "DienTichBuLong," +
                                                               "ChieuDayMu," +
                                                               "ChieuDayDaiOc," +
                                                               "DuongKinhTamDem," +
                                                               "ChieuDayBeTong," +
                                                               "ChieuCaoMuiTon," +
                                                               "ChieuDayLopBaoVe," +
                                                               "HamLuongCotThep," +
                                                               "DoCung," +
                                                               "KhoangCach)" +
                                                               "VALUES('" +
                                                               thongso.TenThongSo + "','" +
                                                               thongso.ChieuCaoDuongHanBung + "','" +
                                                               thongso.ChieuCaoDuongHanCanh + "','" +
                                                               thongso.BuLongDenMepTrenSuon + "','" +
                                                               thongso.BuLongDenMepDuoiSuon + "','" +
                                                               thongso.BuLongDenMepNgoaiCanhCot + "','" +
                                                               thongso.BuLongPhuongNgang + "','" +
                                                               thongso.BuLongPhuongDung + "','" +
                                                               thongso.BeRongSuon + "','" +
                                                               thongso.ChieuDaySuon + "','" +
                                                               thongso.DienTichBuLong + "','" +
                                                               thongso.ChieuDayMu + "','" +
                                                               thongso.ChieuDayDaiOc + "','" +
                                                               thongso.DuongKinhTamDem + "','" +
                                                               thongso.ChieuDayBeTong + "','" +
                                                               thongso.ChieuCaoMuiTon + "','" +
                                                               thongso.ChieuDayLopBaoVe + "','" +
                                                               thongso.HamLuongCotThep + "','" +
                                                               thongso.DoCung + "','" +
                                                               thongso.KhoangCach + "')";
                cmd.CommandType = CommandType.Text;
                conn.Open();

                cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }
        }

        public void Delete(int id)
        {
            try
            {
                cmd.CommandText = "DELETE FROM ThongSoLienKet WHERE ThongSoLienKetID = " + id; 
                cmd.CommandType = CommandType.Text;
                conn.Open();

                cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }
        }

        public void Delete(ThongSoLienKet m)
        {

            try
            {
                cmd.CommandText = "DELETE FROM KetQua WHERE ThongSoLienKetID = " + m.ThongSoLienKetID;
                cmd.CommandType = CommandType.Text;
                conn.Open();

                cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }

        }

        public KetQua FindKetQua(ThongSoLienKet m)
        {
            try
            {
                Connect();
                KetQua ketqua = new KetQua();
                cmd.CommandText = "SELECT * FROM KetQua WHERE ThongSoLienKetID = " + m.ThongSoLienKetID;
                cmd.CommandType = CommandType.Text;
                conn.Open();
                OleDbDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    ketqua.KetQuaID = int.Parse(reader["KetQuaID"].ToString());
                }
                return ketqua;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }

        }

        #endregion

        #region Form Events

        private void FrmThongSoLienKet_Load(object sender, EventArgs e)
        {
            Connect();
            grcThongSoLienKet.DataSource = LoadThongSoLienKet();
            ActiveButton(true, false, false, false);
            ActiveText(false);
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            ActiveText(true);
            ActiveButton(false, true, false, true);
            CleanText();
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            Connect();
            ActiveText(false);
            ActiveButton(true, false, false, false);
            CleanText();
            grcThongSoLienKet.DataSource = LoadThongSoLienKet();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            Connect();
            if (
                txtTenThongSoLienKet.Text == "" ||
            txtBeRongSuon.Text == "" ||
            txtChieuCaoDuongHanBung.Text == "" ||
            txtChieuCaoDuongHanCanh.Text == "" ||
            txtChieuCaoMuiTonSan.Text == "" ||
            txtChieuDayBeTong.Text == "" ||
            txtChieuDayDaiOc.Text == "" ||
            txtChieuDayLopBaoVe.Text == "" ||
            txtChieuDayMu.Text == "" ||
            txtChieuDaySuon.Text == "" ||
            txtDienTichBuLong.Text == "" ||
            txtDoCung.Text == "" ||
            txtDuongKinhTamDem.Text == "" ||
            txtHamLuongCotThepSan.Text == "" ||
            txtKhoangCach.Text == "" ||
            txtKhoangCachBuLongCanhCot.Text == "" ||
            txtKhoangCachBuLongDung.Text == "" ||
            txtKhoangCachBulongNgang.Text == "" ||
            txtKhoangCachBuLongSuonDuoi.Text == "" ||
            txtKhoangCachBuLongSuonTren.Text == ""
                ) { MessageBox.Show("Chưa nhập đủ dữ liệu!"); return; }

            ThongSoLienKet thongso = new ThongSoLienKet();

            thongso.TenThongSo = txtTenThongSoLienKet.Text;
            thongso.ChieuCaoDuongHanBung = float.Parse(txtChieuCaoDuongHanBung.Text);
            thongso.ChieuCaoDuongHanCanh = float.Parse(txtChieuCaoDuongHanCanh.Text);
            thongso.BuLongDenMepTrenSuon = float.Parse(txtKhoangCachBuLongSuonTren.Text);
            thongso.BuLongDenMepDuoiSuon = float.Parse(txtKhoangCachBuLongSuonDuoi.Text);
            thongso.BuLongDenMepNgoaiCanhCot = float.Parse(txtKhoangCachBuLongCanhCot.Text);
            thongso.BuLongPhuongNgang = float.Parse(txtKhoangCachBulongNgang.Text);
            thongso.BuLongPhuongDung = float.Parse(txtKhoangCachBuLongDung.Text);
            thongso.BeRongSuon = float.Parse(txtBeRongSuon.Text);
            thongso.ChieuDaySuon = float.Parse(txtChieuDaySuon.Text);
            thongso.DienTichBuLong = float.Parse(txtDienTichBuLong.Text);
            thongso.ChieuDayMu = float.Parse(txtChieuDayMu.Text);
            thongso.ChieuDayDaiOc = float.Parse(txtChieuDayDaiOc.Text);
            thongso.DuongKinhTamDem = float.Parse(txtDuongKinhTamDem.Text);
            thongso.ChieuDayBeTong = float.Parse(txtChieuDayBeTong.Text);
            thongso.ChieuCaoMuiTon = float.Parse(txtChieuCaoMuiTonSan.Text);
            thongso.ChieuDayLopBaoVe = float.Parse(txtChieuDayLopBaoVe.Text);
            thongso.HamLuongCotThep = float.Parse(txtHamLuongCotThepSan.Text);
            thongso.DoCung = float.Parse(txtDoCung.Text);
            thongso.KhoangCach = float.Parse(txtKhoangCach.Text);

            Insert(thongso);
            ActiveText(false);
            ActiveButton(true, false, false, false);
            CleanText();
            grcThongSoLienKet.DataSource = LoadThongSoLienKet();
        }

        private void grvThongSoLienKet_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            Connect();
            if (grvThongSoLienKet.SelectedRowsCount > 0)
            {
                ThongSoLienKet thongso = FindThongSoLienKet((int)grvThongSoLienKet.GetRowCellValue(grvThongSoLienKet.GetSelectedRows()[0], "ThongSoLienKetID"));
                if (thongso != null)
                {
                    thongsoID = thongso.ThongSoLienKetID.ToString();
                    ThongSoID = thongso.ThongSoLienKetID.ToString();
                    txtTenThongSoLienKet.Text = thongso.TenThongSo;
                    txtBeRongSuon.Text = thongso.BeRongSuon.ToString();
                    txtChieuCaoDuongHanBung.Text = thongso.ChieuCaoDuongHanBung.ToString();
                    txtChieuCaoDuongHanCanh.Text = thongso.ChieuCaoDuongHanCanh.ToString();
                    txtChieuCaoMuiTonSan.Text = thongso.ChieuCaoMuiTon.ToString();
                    txtChieuDayBeTong.Text = thongso.ChieuDayBeTong.ToString(); ;
                    txtChieuDayDaiOc.Text = thongso.ChieuDayDaiOc.ToString();
                    txtChieuDayLopBaoVe.Text = thongso.ChieuDayLopBaoVe.ToString();
                    txtChieuDayMu.Text = thongso.ChieuDayMu.ToString();
                    txtChieuDaySuon.Text = thongso.ChieuDaySuon.ToString();
                    txtDienTichBuLong.Text = thongso.DienTichBuLong.ToString();
                    txtDoCung.Text = thongso.DoCung.ToString();
                    txtDuongKinhTamDem.Text = thongso.DuongKinhTamDem.ToString();
                    txtHamLuongCotThepSan.Text = thongso.HamLuongCotThep.ToString();
                    txtKhoangCach.Text = thongso.KhoangCach.ToString();
                    txtKhoangCachBuLongCanhCot.Text = thongso.BuLongDenMepNgoaiCanhCot.ToString();
                    txtKhoangCachBuLongDung.Text = thongso.BuLongPhuongDung.ToString();
                    txtKhoangCachBulongNgang.Text = thongso.BuLongPhuongNgang.ToString();
                    txtKhoangCachBuLongSuonDuoi.Text = thongso.BuLongDenMepDuoiSuon.ToString();
                    txtKhoangCachBuLongSuonTren.Text = thongso.BuLongDenMepTrenSuon.ToString();

                    ActiveButton(true, false, true, true);
                }
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            Connect();

            if (ThongSoID != "")
            {
                ThongSoLienKet thep = FindThongSoLienKet(int.Parse(ThongSoID));
                if (thep != null)
                {
                    KetQua k = FindKetQua(thep);
                    if (k.KetQuaID != 0)
                    {
                        DialogResult rs = MessageBox.Show("Thông số này đã được dùng để tính toán, nếu bạn xóa thông số này, chương trình sẽ xóa cả kết quả dùng thông số này để tính toán!! Bạn có chắc muốn xóa??", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                        if (rs == DialogResult.Yes)
                        {
                            Delete(thep.ThongSoLienKetID);
                            Delete(thep);
                            
                        }
                    }
                    else { 
                        Delete(thep.ThongSoLienKetID);
                    }

                    ActiveText(false);
                    ActiveButton(true, false, false, false);
                    CleanText();
                    grcThongSoLienKet.DataSource = LoadThongSoLienKet();
                }
            }
            else
                MessageBox.Show("Thông Số Liên Kết Không tồn tại.");
        }

        #endregion
    }
}